# coding:utf-8
from collections import defaultdict

import jieba

# 情感词典分析的是整个文章的情感倾向。
# s1: 数据清洗，删除停用词（将停用词加载到内存，dict/stopwords），包括标点符号
# s2: 分词（jieba分词）
# s3: 查表统计
class SentimentDict(object):
    def __init__(self):
        self.loadStopwords()
        self.loadSentiWords()

    @classmethod
    def loadStopwords(cls):
        stopwords_path = 'dict/stopwords.txt'
        userdict_path = 'dict/userdict.txt'
        cls.stopwords = []
        jieba.load_userdict(userdict_path) # 用户新词
        with open(stopwords_path, 'r', encoding='utf-8') as file:
            cls.stopwords = [line.strip() for line in file.readlines()]

        # frequency = defaultdict(int)
        # l = list(jieba.cut(sentence))
        # for t in l:
        #     frequency[t] += 1
        #
        # texts = [token for token in frequency if frequency[token] > 0]
        #
        # rtexts = list(set(texts) - set(stropw))
    # 加载情感词典
    def loadSentiWords(self):
        # 情感词典
        self.posdict = self.deal_wrap('dict/emotion_dict/pos_all_dict.txt')
        self.negdict = self.deal_wrap('dict/emotion_dict/neg_all_dict.txt')
        # 程度副词词典
        self.mostdict = self.deal_wrap('dict/degree_dict/most.txt')  # 权值为2
        self.verydict = self.deal_wrap('dict/degree_dict/very.txt')  # 权值为1.5
        self.moredict = self.deal_wrap('dict/degree_dict/more.txt')  # 权值为1.25
        self.ishdict = self.deal_wrap('dict/degree_dict/ish.txt')  # 权值为0.5
        self.insufficientdict = self.deal_wrap('dict/degree_dict/insufficiently.txt')  # 权值为0.25
        self.inversedict = self.deal_wrap('dict/degree_dict/inverse.txt')  # 权值为-1

    def deal_wrap(self, filedict):
        temp = []
        for x in open(filedict, 'r', encoding='utf-8').readlines():
            temp.append(x.strip())
        return temp

    def cal_score(self, word, sentence_score):
        if word in self.mostdict:
            sentence_score *= 2.0
        elif word in self.verydict:
            sentence_score *= 1.75
        elif word in self.moredict:
            sentence_score *= 1.5
        elif word in self.ishdict:
            sentence_score *= 1.2
        elif word in self.insufficientdict:
            sentence_score *= 0.5
        elif word in self.inversedict:
            sentence_score *= -1
        return sentence_score

if __name__ == '__main__':
    s = SentimentDict()
    # print(s.stopwords[:20])
    # print(s.posdict[:20])
    # print(s.negdict[:20])
    # print(s.mostdict[:20])

    frequency = defaultdict(int)
    l = list(jieba.cut('就把定义精神疾病看作是一种权力。他认为，精神疾病能够定义谁正常、谁不正常；'))
    for t in l:
        frequency[t] += 1
    print(frequency)
    texts = [token for token in frequency if frequency[token] > 0]
    print(texts)
    rtexts = list(set(texts) - set(s.stopwords))
    print(rtexts)
